import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from  '@/views/Login.vue'
import Home from '@/views/Home.vue'
import UserList from '@/views/user/UserList.vue'
import RoleList from '@/views/role/RoleList.vue'
import Menus from '@/views/menu/Menus.vue'

Vue.use(VueRouter)

const routes = [
  {path:'/',redirect:'/login'},
  {name:'登录',path:'/login',component:Login},
  {name:'主页',path:'/home',component:Home,children:[
      {name:'用户管理',path:'/userList',component:UserList},
      {name:'角色管理',path:'/roleList',component:RoleList},
      {name:'菜单管理',path:'/menus',component:Menus}
    ]
  },

]

const router = new VueRouter({
  routes
})

router.beforeEach(function (to, from, next) {//to 路由目标 //from  //next 拦截补拦截，拦截到哪 路由守卫

  if(to.path=='/login'){ //当访问登录组件不拦截
    return next();
  }

  let user=sessionStorage.getItem("user");//获取sessionStorage中用户的信息
  if(user){
    //当前用户已登录
    return next();//继续访问
  }

  return  next('/login');
})


export default router
